package com.lhkj.ct.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.config.FileServerConfig;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.OperatorType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.base.utils.file.FileUploadUtils;
import com.lhkj.ct.base.utils.file.MimeTypeUtils;
import com.lhkj.ct.framework.auth.dto.WeChatBindDto;
import com.lhkj.ct.meta.modules.patient.model.dto.PatBasicInfo;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatCardRelation;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatient;
import com.lhkj.ct.meta.modules.patient.service.PatientService;
import com.lhkj.ct.wechat.base.model.dto.PatientBindDto;
import com.lhkj.ct.wechat.service.WxMpPersonalCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/wechat/personal/center")
@Api(tags = {"5.用户中心"})
@Validated
public class WxMpMineController {

    @Resource
    private WxMpPersonalCenterService wxMpPersonalCenterService;

    @Resource
    private PatientService patientService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "个人信息")
    @GetMapping("profile")
    public ActionResult<PatBasicInfo> profile() {
        return ActionResult.success(patientService.getBasicInfo());
    }

    /**
     * 头像上传
     */
    @ApiOperation("上传头像")
    @ApiOperationSupport(order = 2)
    @Log(title = "用户头像", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    @PreventDuplicateSubmit
    @PostMapping("/avatar")
    public ActionResult<?> avatar(@RequestParam("avatar") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(FileServerConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (patientService.updateUserAvatar(ShiroUtil.getAuthUserInfo().getUserId(), avatar)) {
                Map<String, String> map = new HashMap<>();
                map.put("imgUrl", avatar);
                return ActionResult.success(map);
            }
        }
        return ActionResult.fail("上传图片异常，请联系管理员");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "更新个人信息")
    @PutMapping("updateBasicInfo")
    @PreventDuplicateSubmit
    @Log(title = "个人中心", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    public ActionResult<Void> profile(@Validated @RequestBody PatBasicInfo info) {
        return ActionResult.judge(patientService.updateBasicInfo(info));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改手机号")
    @PutMapping("updatePhone")
    @PreventDuplicateSubmit
    @Log(title = "个人中心", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    public ActionResult<Void> updatePhone(@Validated(PatientBindDto.Mobile.class) @RequestBody PatientBindDto mobile) {
        return ActionResult.judge(patientService.getBaseMapper().update(new LambdaUpdateWrapper<TblPatient>()
                .eq(TblPatient::getId, ShiroUtil.getAuthUserInfo().getUserId())
                .set(TblPatient::getPatMobile, mobile.getPatMobile())
        ));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改邮箱")
    @PutMapping("updateEmail")
    @PreventDuplicateSubmit
    @Log(title = "个人中心", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    public ActionResult<Void> updateEmail(@Validated(PatientBindDto.Email.class) @RequestBody PatientBindDto email) {
        return ActionResult.judge(patientService.getBaseMapper().update(new LambdaUpdateWrapper<TblPatient>()
                .eq(TblPatient::getId, ShiroUtil.getAuthUserInfo().getUserId())
                .set(TblPatient::getPatEmail, email.getPatEmail())
        ));
    }

    @ApiOperation(value = "绑定就诊卡")
    @ApiOperationSupport(order = 1)
    @PostMapping("bindCardNo")
    public ActionResult<Void> bindCardNo(@RequestBody WeChatBindDto bindDto) {
        wxMpPersonalCenterService.bindPatCardNo(bindDto);
        return ActionResult.success("绑定成功");
    }

    @ApiOperation(value = "解绑就诊卡")
    @ApiOperationSupport(order = 1)
    @PutMapping("{id}/unbindCardNo")
    public ActionResult<Void> unbindCardNo(@PathVariable String id) {
        return ActionResult.judge(wxMpPersonalCenterService.unbindPatCardNo(id));
    }

    @ApiOperation(value = "切换就诊卡")
    @ApiOperationSupport(order = 1)
    @PutMapping("{id}/switchPatCard")
    public ActionResult<PatBasicInfo> switchPatCard(@PathVariable String id) {
        wxMpPersonalCenterService.switchPatCard(id);
        return ActionResult.success(patientService.getBasicInfo());
    }

    @ApiOperation(value = "就诊卡列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("cardNoList")
    public ActionResult<List<TblPatCardRelation>> cardNoList() {
        return ActionResult.success(wxMpPersonalCenterService.selectCardNoByUser());
    }

    @ApiOperation(value = "微信解绑")
    @ApiOperationSupport(order = 1)
    @DeleteMapping("unbindWechat")
    public ActionResult<Void> unbindWechat() {
        boolean res = wxMpPersonalCenterService.unbindWechat();
        if (res) {
            SecurityUtils.getSubject().logout();
        }
        return ActionResult.judge(res);
    }
}
